package com.itheima.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.book.interceptor.BaseContext;
import com.itheima.book.pojo.dto.BooksPageQueryDTO;
import com.itheima.book.pojo.entity.Book;
import com.itheima.book.service.BookService;
import com.itheima.book.mapper.BookMapper;
import com.itheima.book.utils.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

/**
* @author 86178
* @description 针对表【book】的数据库操作Service实现
* @createDate 2025-04-20 18:34:28
*/
@Slf4j
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book>
    implements BookService {
    @Autowired
    private BookMapper bookMapper;

    @Override
    public List<Book> getHotBook() {
        // 查询最新发布的6本书籍，且状态为"在售"
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Book::getStatus, "在售");
        wrapper.orderByDesc(Book::getCreateTime);
        wrapper.last("LIMIT 6"); // 限制返回6条记录
        List<Book> hotBooks = bookMapper.selectList(wrapper);
        return hotBooks;
    }

    @Override
    public PageHelper<Book> getmarket(BooksPageQueryDTO booksPageQueryDTO) {
        // 计算偏移量
        int offset = (booksPageQueryDTO.getCurrentPage() - 1) * booksPageQueryDTO.getPageSize();
        
        // 获取查询条件
        String searchQuery = booksPageQueryDTO.getSearchQuery();
        String sortBy = booksPageQueryDTO.getSortBy();
        
        // 查询总记录数
        int total = bookMapper.selectTotalCount(searchQuery);
        
        // 查询当前页数据
        List<Book> records = bookMapper.selectBookList(offset, booksPageQueryDTO.getPageSize(), searchQuery, sortBy);
        
        // 封装返回分页对象
        PageHelper<Book> pageHelper = new PageHelper<>(booksPageQueryDTO.getCurrentPage(), booksPageQueryDTO.getPageSize());
        pageHelper.setTotal(total);
        pageHelper.setList(records);
        
        return pageHelper;
    }

    @Override
    public PageHelper<Book> getUserId(BooksPageQueryDTO queryDTO) {
        // 从线程中获取id
        Long currentId = BaseContext.getCurrentId();
        log.info("当前用户ID: {}", currentId);
        
        // 计算偏移量
        int offset = (queryDTO.getCurrentPage() - 1) * queryDTO.getPageSize();
        
        // 查询总记录数
        int total = bookMapper.selectUserBookCount(currentId, queryDTO.getSearchQuery());
        
        // 查询当前页数据
        List<Book> records = bookMapper.selectUserBookList(
            currentId, 
            offset, 
            queryDTO.getPageSize(), 
            queryDTO.getSearchQuery(), 
            queryDTO.getSortBy()
        );
        
        log.info("查询结果总数: {}", total);
        log.info("当前页数据: {}", records);
        
        // 封装返回分页对象
        PageHelper<Book> pageHelper = new PageHelper<>(queryDTO.getCurrentPage(), queryDTO.getPageSize());
        pageHelper.setTotal(total);
        pageHelper.setList(records);
        
        return pageHelper;
    }

    @Override
    public void savebook(Book book) {
        book.setUpdateTime(new Date());
        book.setCreateTime(new Date());
        book.setSellerId(BaseContext.getCurrentId());
        book.setStatus("在售");
        bookMapper.insert(book);
    }

    @Override
    public PageHelper<Book> getBookList(BooksPageQueryDTO queryDTO) {
        // 计算偏移量
        int offset = (queryDTO.getCurrentPage() - 1) * queryDTO.getPageSize();
        
        // 查询总记录数
        int total = bookMapper.selectTotalCount(queryDTO.getSearchQuery());
        
        // 查询当前页数据
        List<Book> records = bookMapper.selectBookList(
            offset, 
            queryDTO.getPageSize(), 
            queryDTO.getSearchQuery(), 
            queryDTO.getSortBy()
        );
        
        // 封装返回分页对象
        PageHelper<Book> pageHelper = new PageHelper<>(queryDTO.getCurrentPage(), queryDTO.getPageSize());
        pageHelper.setTotal(total);
        pageHelper.setList(records);
        
        return pageHelper;
    }

    @Override
    public Book getById(Long id) {
        return bookMapper.selectById(id);
    }

    @Override
    public boolean updateById(Book book) {
        return bookMapper.updateById(book) > 0;
    }

    @Override
    public void removeById(Long id) {
        bookMapper.deleteById(id);
    }
}




